#!/bin/bash

procs=$1
# procs=`echo "($procs*2)" | bc`

current=`pwd`

LD_LIB_PATH="$current/../build_lib"

LOCK_LIBS="mutex
pspin
spin
ticket
clh
mcs
rcl
ffwd
sffwd
cacsffwd
flat
cacflat
ccsync
cacccsyn
dsmsync
cacdsmsyn
hsync
cachsyn"

timecmd=/usr/bin/time
AARCH64_PLATFORM=`uname -a | grep "aarch64"`
if [ ! -n "$AARCH64_PLATFORM" ] ; then
	NICE=""
else
	NICE="nice -n -20"
fi
output_file=${current}/test_out.tsv
# /RAYTRACE -p$procs -m64 inputs/car.env
# raytrace_car
echo "benchmarks libs time rss user sys pagefault reclaim" > $output_file

function test_raytrace_car {
	cd ${current}
	cd Splash-3/codes/apps/raytrace
	$timecmd -a -f "raytrace_car $1 %e %M %U %S %F %R" ${NICE} env LD_PRELOAD=$2 ./RAYTRACE -p$procs -m128 inputs/car_512.env 1> /dev/null 2> output_test.txt
	# cat output_test.txt 
	tail -n1 output_test.txt
	tail -n1 output_test.txt >> $output_file
}

function test_raytrace_teapot {
	cd ${current}
	cd Splash-3/codes/apps/raytrace
	$timecmd -a -f "raytrace_teapot $1 %e %M %U %S %F %R" ${NICE}  env LD_PRELOAD=$2 ./RAYTRACE -p$procs -m64 inputs/teapot_512.env 1> /dev/null 2> output_test.txt
	# cat output_test.txt 
	tail -n1 output_test.txt
	tail -n1 output_test.txt >> $output_file
}
function test_raytrace_balls4 {
	cd ${current}
	cd Splash-3/codes/apps/raytrace
	$timecmd -a -f "raytrace_balls4 $1 %e %M %U %S %F %R" ${NICE}  env LD_PRELOAD=$2 ./RAYTRACE -p$procs -m64 inputs/balls4_512.env 2> output_test.txt
	# cat output_test.txt 
	tail -n1 output_test.txt
	tail -n1 output_test.txt >> $output_file
}
function test_barnes_sim {
	cd ${current}
	cd Splash-3/codes/apps/barnes
	echo "
131072
123

0.025
0.05
1.0
2.0
5.0
0.075
0.25
$procs" > inputs/lock_test
	$timecmd -a -f "barnes $1 %e %M %U %S %F %R" ${NICE} env LD_PRELOAD=$2 ./BARNES < inputs/lock_test 1> /dev/null 2> output_test.txt
	# cat output_test.txt
	tail -n1 output_test.txt
	tail -n1 output_test.txt >> $output_file
}
function test_barnes_sim_large {
	cd ${current}
	cd Splash-3/codes/apps/barnes
	echo "
262144
123

0.025
0.05
1.0
2.0
5.0
0.075
0.25
$procs" > inputs/lock_test1
	$timecmd -a -f "barnes_large $1 %e %M %U %S %F %R" ${NICE} env LD_PRELOAD=$2 ./BARNES < inputs/lock_test1 1> /dev/null 2> output_test.txt
	# cat output_test.txt 
	tail -n1 output_test.txt
	tail -n1 output_test.txt >> $output_file
}
# 有BUG!!(可能是递归的锁获取?)
function test_radiosity {
	cd ${current}
	cd Splash-3/codes/apps/radiosity
	$timecmd -a -f "radiosity $1 %e %M %U %S %F %R" ${NICE} env LD_PRELOAD=$2 ./RADIOSITY -p $nproc -ae 5000 -bf 0.1 -en 0.05 -room -batch 1> /dev/null 2> output_test.txt
	# cat output_test.txt 
	tail -n1 output_test.txt
	tail -n1 output_test.txt >> $output_file
}

function do_test {
	echo "$1"
	for lks in $LOCK_LIBS
	do
		# echo "$LD_LIB_PATH/$lks.so"
		$1 $lks ${LD_LIB_PATH}/glibapfsds_${lks}.so
	done
	for lks in $LOCK_LIBS
	do
		# echo "$LD_LIB_PATH/$lks.so"
		$1 $lks ${LD_LIB_PATH}/cllibapfsds_${lks}.so
	done
}

trap "exit" INT
# for lks in $LOCK_LIBS
# do
#	 # echo "$lks $LD_LIB_PATH/$lks"
#	 test_raytrace_car $lks $LD_LIB_PATH/$lks
# done
# do_test test_raytrace_teapot
for ((i=1; i<=$2; i++))
do
	do_test test_barnes_sim
	# do_test test_barnes_sim_large
	# do_test test_radiosity
	do_test test_raytrace_teapot
	# do_test test_raytrace_balls4
	do_test test_raytrace_car
done

cd $current
cat $output_file
